package com.example.blogserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blogserver.a_utils.Constant;
import com.example.blogserver.a_utils.ResponseBodyMessage;
import com.example.blogserver.mapper.UserMapper;
import com.example.blogserver.pojo.User;
import com.example.blogserver.service.AdminService;
import com.example.blogserver.service.ListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * @description
 * @descriptionDetail
 * @Author B3300
 * @copyright 摸鱼王胖嘟嘟
 * @Date 2024/3/19 21:58
 */
@Service
public class AdminServiceImpl extends ServiceImpl<UserMapper, User> implements AdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ListService listService;

    @Override
    public ResponseBodyMessage<User> getNowUser(HttpServletRequest request) {
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        HttpSession session = request.getSession(false);
        String account = (String) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        User user = userMapper.queryUser(account);
        if (user == null) {
            responseBodyMessage.setStatus(-1);
            responseBodyMessage.setMessage("当前未登录");
            responseBodyMessage.setData(null);
        } else {
            responseBodyMessage.setStatus(1);
            responseBodyMessage.setMessage("用户已经登录");
            responseBodyMessage.setData(user);
        }
        return responseBodyMessage;
    }

    @Override
    public ResponseBodyMessage<List<User>> queryUserAll(HttpServletRequest request) {
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        String account = (String) request.getSession(false).getAttribute(Constant.USERINFO_SESSION_KEY);
        User user = userMapper.queryUser(account);
        List<User> users = new ArrayList<>();
        if (user.getIdentity() == 1) {
            users = userMapper.queryUserExceptOwn(account);
        }
        if (users.size() == 0) {
            responseBodyMessage.setStatus(-1);
            responseBodyMessage.setMessage("当前还没有其他用户~");
            responseBodyMessage.setData(null);
        } else {
            responseBodyMessage.setStatus(1);
            responseBodyMessage.setMessage("获取成功");
            responseBodyMessage.setData(users);
        }
        return responseBodyMessage;
    }

    @Override
    public ResponseBodyMessage<Page> page(int page, int pageSize) {
        // 查询所有用户信息
        Page pageInfo = new Page(page, pageSize);
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(User::getUserid); // 按照用户ID降序排序
        responseBodyMessage.setStatus(1);
        responseBodyMessage.setMessage("获取成功");
        responseBodyMessage.setData(this.page(pageInfo, lambdaQueryWrapper));
        return responseBodyMessage;
    }

    @Override
    public ResponseBodyMessage<Boolean> updateIdentityByUserId(HttpServletRequest request, int identity, String id) {
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        HttpSession session = request.getSession(false);
        String account = (String) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        User user = userMapper.queryUser(account);
        if (user.getIdentity() == 2) {
            if (identity == 1) {
                userMapper.updateIdentityByUserId(0, id);
            } else {
                userMapper.updateIdentityByUserId(1, id);
            }
            responseBodyMessage.setStatus(1);
            responseBodyMessage.setMessage("修改用户身份权限成功！");
            responseBodyMessage.setData(true);
        } else {
            responseBodyMessage.setStatus(-1);
            responseBodyMessage.setMessage("该用户不是超级管理员");
            responseBodyMessage.setData(false);
        }
        return responseBodyMessage;
    }
}
